Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | /**
* Profile Validation Middleware
*
* Validates profile customization data including colors.
* Ensures data integrity and security for profile operations.
*
* @module middleware/validators/profileValidation
*/
const { body, validationResult } = require('express-validator');
const { logger } = require('../../config/logger');
/**
* Validation rules for updating colors
*/
const validateColors = [
body('primary_color')
.optional()
.trim()
.matches(/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/)
.withMessage('Invalid primary color format. Use hex format (e.g., #00a149)'),
body('primary_dark')
.optional()
.trim()
.matches(/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/)
.withMessage('Invalid primary dark color format. Use hex format (e.g., #654321)'),
body('primary_light')
.optional()
.trim()
.matches(/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/)
.withMessage('Invalid primary light color format. Use hex format (e.g., #A0522D)'),
body('accent_color')
.optional()
.trim()
.matches(/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/)
.withMessage('Invalid accent color format. Use hex format (e.g., #CD853F)'),
body('text_color')
.optional()
.trim()
.matches(/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/)
.withMessage('Invalid text color format. Use hex format (e.g., #333333)'),
body('text_light')
.optional()
.trim()
.matches(/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/)
.withMessage('Invalid text light color format. Use hex format (e.g., #666666)'),
body('bg_color')
.optional()
.trim()
.matches(/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/)
.withMessage('Invalid background color format. Use hex format (e.g., #f5f5f5)'),
body('white')
.optional()
.trim()
.matches(/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/)
.withMessage('Invalid white color format. Use hex format (e.g., #ffffff)'),
body('success')
.optional()
.trim()
.matches(/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/)
.withMessage('Invalid success color format. Use hex format (e.g., #28a745)'),
body('warning')
.optional()
.trim()
.matches(/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/)
.withMessage('Invalid warning color format. Use hex format (e.g., #ffc107)'),
body('danger')
.optional()
.trim()
.matches(/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/)
.withMessage('Invalid danger color format. Use hex format (e.g., #dc3545)'),
body('info')
.optional()
.trim()
.matches(/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/)
.withMessage('Invalid info color format. Use hex format (e.g., #17a2b8)'),
// Validation result handler
(req, res, next) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
logger.warn('Profile color validation failed', {
errors: errors.array(),
userId: req.user?.id,
tenantId: req.user?.tenantId
});
return res.status(400).json({
success: false,
error: 'Validation failed',
details: errors.array().map(err => ({
field: err.path,
message: err.msg
}))
});
}
next();
}
];
module.exports = {
validateColors
};
|